package com.jinzheng;

import com.jinzheng.constants.PetConstant;
import com.jinzheng.model.Dog;
import com.jinzheng.model.Penguin;
import com.jinzheng.model.Pet;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        try {
            System.out.println("欢迎来到XXX宠物店，请选择你要领养的宠物：1.狗狗\t2.企鹅");
            Scanner scanner = new Scanner(System.in);
            Pet pet = new Pet(PetConstant.PET_LIVING);
            Dog dog;
            Penguin penguin;
            int petType = scanner.nextInt();            //用户选择宠物的类型
            if (petType == PetConstant.PET_DOG){
                System.out.println("你选择的是狗狗");
                System.out.println("请输入狗狗的名字");
                String dogName = scanner.next();           //用户输入狗狗的名字
                if (null != dogName && ""!= dogName){
                    pet.setName(dogName);
                    pet = chooseStrain(pet,scanner);
                    showInfo(pet);
                    dog = new Dog();            //新建dog实例
                    dog.setName(pet.getName());
                    dog.setStrain(pet.getStrain());
                    dog.setLiving(pet.getLiving());
                    dog.setWight(PetConstant.DOG_WEIGHT);
                    while (true){
                        dogOption(dog,scanner);
                    }
                }else {
                    System.out.println("对不起，宠物的名字不能为空哦！");
                }
            }else if (petType == PetConstant.PET_PENGUIN){
                System.out.println("你选择的是企鹅");
                System.out.println("请输入企鹅的名字");
                String penguinName = scanner.next();
                if (null != penguinName && ""!= penguinName){
                    pet.setName(penguinName);
                    System.out.println("请选择要领养宠物的品种（1.南极的企鹅\t2.北极的企鹅）");
                    int penguinStrain = scanner.nextInt();
                    if (penguinStrain == PetConstant.PET_CHOOSE_NAN){
                        pet.setStrain(PetConstant.PET_STRAIN_NAN);
                    }else if (penguinStrain == PetConstant.PET_CHOOSE_BEI){
                        pet.setStrain(PetConstant.PET_STRAIN_BEI);
                    }else {
                        System.out.println("对不起，没有该品种");
                    }
                    showInfo(pet);
                    penguin = new Penguin();
                    penguin.setName(pet.getName());
                    penguin.setStrain(pet.getStrain());
                    penguin.setLiving(pet.getLiving());
                    penguin.setHeight(PetConstant.PENGUIN_HEIGHT);
                    while (true){
                        penguinOption(penguin,scanner);
                    }
                }else {
                    System.out.println("对不起，宠物的名字不能为空哦！");
                }
            }else {
                System.out.println("对不起，没有你要领养的宠物");
            }
        } catch (Exception e) {
            System.out.println("输入错误");
        }
    }

    /**
     * 狗狗操作
     * @param dog
     * @param scanner
     */
    private static Dog dogOption(Dog dog,Scanner scanner) {
        System.out.println("你想对你的宠物干啥：1.喂食\t2.踹它\t3.查看");
        int option = scanner.nextInt();
        if (option == PetConstant.PET_FOOD){
            //喂食方法  生命值最大100 不足100每次加10 体重加10
            dog.setLiving(dog.getLiving()+PetConstant.PET_OPTION_LIVING);
            dog.setWight(dog.getWight()+PetConstant.DOG_OPTION_WEIGHT);
            System.out.println("宠物喂食成功,健康加"+PetConstant.PET_OPTION_LIVING+",体重加"+PetConstant.DOG_OPTION_WEIGHT);
            showInfo(dog);
        }else if (option == PetConstant.PET_FIGHT){
            //踹它方法  生命值最小0 为0时游戏结束
            int current_living = dog.getLiving() - PetConstant.PET_OPTION_LIVING;
            if (current_living == PetConstant.PET_LIVING_DEATH){
                System.out.println("游戏结束");
                System.exit(0);
            }
            dog.setLiving(current_living);
            dog.setWight(dog.getWight()-PetConstant.DOG_OPTION_WEIGHT);
            showInfo(dog);

        }else if (option == PetConstant.PET_INFO){
            //查看方法
            showInfo(dog);
        }

        return dog;
    }

    /**
     * 企鹅操作
     * @param penguin
     * @param scanner
     * @return
     */
    private static Penguin penguinOption(Penguin penguin, Scanner scanner) {
        System.out.println("你想对你的宠物干啥：1.喂食\t2.踹它\t3.查看");
        int option = scanner.nextInt();
        if (option == PetConstant.PET_FOOD){
            //投食的方法
            penguin.setLiving(penguin.getLiving()+PetConstant.PET_OPTION_LIVING);
            penguin.setHeight(penguin.getHeight()+PetConstant.PENGUIN_OPTION_HEIGHT);
            System.out.println("宠物喂食成功,健康加"+PetConstant.PET_OPTION_LIVING+",身高加"+PetConstant.PENGUIN_OPTION_HEIGHT);
            showInfo(penguin);
        }else if (option == PetConstant.PET_FIGHT){
            //踹它方法
            int current_living = penguin.getLiving() - PetConstant.PET_OPTION_LIVING;
            if (current_living == PetConstant.PET_LIVING_DEATH){
                System.out.println("游戏结束！");
                System.exit(0);
            }
            penguin.setLiving(current_living);
            penguin.setHeight(penguin.getHeight() - PetConstant.PENGUIN_OPTION_HEIGHT);
            showInfo(penguin);
        }else if (option == PetConstant.PET_INFO){
            //查看方法
            showInfo(penguin);
        }
        return penguin;
    }

    private static Pet chooseStrain(Pet pet,Scanner scanner) {

        System.out.println("请选择要领养宠物的品种（1.聪明的拉布拉多\t2.愚蠢的哈士奇）");
        int dogStrain = scanner.nextInt();
        if (dogStrain == PetConstant.PET_CHOOSE_LBLD){
           pet.setStrain(PetConstant.PET_STRAIN_LBLD);
        }else if (dogStrain == PetConstant.PET_CHOOSE_EH){
           pet.setStrain(PetConstant.PET_STRAIN_EH);
        }else {
            System.out.println("对不起，没有该品种");
        }

        return pet;

    }

    private static void showInfo(Pet pet) {
        System.out.println(pet.toString());
    }
}
